/**
 * Copyright (c) 2013-2016 wisdomsite Ltd. All Rights Reserved.
 *   
 * This software is the confidential and proprietary information of   
 * wisdomsite. You shall not disclose such Confidential Information
 * and shall use it only in accordance with the terms of the agreements   
 * you entered into with wisdomsite.
 */
package com.wh.wisdomsite.xbox.server.task.process;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ScheduledFuture;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * 
* <p>@ClassName: 	TaskDate</p>
* <p>@version:		V 4.0</p>
* <p>@Description:  (task任务数据)</p>
* @author:    		杨君红
* @date:			2016年11月25日 上午9:14:31
 */
public class TaskDate {
	
	private static final Log logger = LogFactory.getLog(TaskDate.class);
	//是否已经执行task
	private static volatile boolean initFlag;
	//存储task
	private static volatile Map<String, Object> scheduledFutureMap;

	public static Map<String, Object> getScheduledFutureMap() {
		return scheduledFutureMap;
	}

	public static void setScheduledFutureMap(
			Map<String, Object> scheduledFutureMap) {
		TaskDate.scheduledFutureMap = scheduledFutureMap;
	}

	public static boolean isInitFlag() {
		return initFlag;
	}

	public static void setInitFlag(boolean initFlag) {
		TaskDate.initFlag = initFlag;
	}
	
	/**
	 * 
	* <p>@Title: 		getTaskInfo</p>
	* <p>@return
	* <p>@throws InterruptedException
	* <p>@throws ExecutionException </p>  
	* <p>@return: 		List<TaskInfo>  </p>
	* <p>@Description: 	(查询task任务)</p>
	*
	* Modification History:
	* Date         Author        Version        Discription
	* ----------------------------------------------------------------
	* 2016年11月25日      杨君红         V4.0           4.0
	* Modify the reason ：
	* Modified to : 
	* @throws
	 */
	public static List <TaskInfo> getTaskInfo() throws InterruptedException, ExecutionException{
		return doGetTaskInfo(0);
	}
	/**
	 * 
	* <p>@Title: 		stop</p>
	* <p>@return </p>  
	* <p>@return: 		List<TaskInfo>  </p>
	* <p>@Description: 	(停止task任务)</p>
	*
	* Modification History:
	* Date         Author        Version        Discription
	* ----------------------------------------------------------------
	* 2016年11月25日      杨君红         V4.0           4.0
	* Modify the reason ：
	* Modified to : 
	* @throws
	 */
	public static synchronized List <TaskInfo> stop() {
		logger.info("task stop,initFlag is " + initFlag);
		List <TaskInfo> listTaskInfos = new ArrayList<TaskInfo>();
		if (initFlag) {
			listTaskInfos = doGetTaskInfo(1);
			initFlag = false;
		}
		return listTaskInfos;
	}
	/**
	 * 
	* <p>@Title: 		doGetTaskInfo</p>
	* <p>@param performType ： 执行方法的标识
	* <p>@return </p>  
	* <p>@return: 		List<TaskInfo>  </p>
	* <p>@Description: 	(执行具体的获取TaskInfo操作)</p>
	*
	* Modification History:
	* Date         Author        Version        Discription
	* ----------------------------------------------------------------
	* 2016年12月1日      杨君红         V4.0           4.0
	* Modify the reason ：
	* Modified to : 
	* @throws
	 */
	private static List <TaskInfo> doGetTaskInfo(int performType) {
		List <TaskInfo> listTaskInfos = new ArrayList<TaskInfo>();		
		for (Map.Entry<String,Object> taskValueMap : scheduledFutureMap.entrySet()) {
			TaskInfo taskInfo = new TaskInfo();
			String key = taskValueMap.getKey();
			String note = "";
			String description = "";
			String strFixedTime = "";
			Long period = new Long(0);
			Long initDelay = new Long(0);
			Integer type = 0;
			boolean status = false;
			if (taskValueMap.getValue() instanceof Map) {
				@SuppressWarnings("unchecked")
				Map <String,Object> taskValue = (Map<String, Object>) taskValueMap.getValue();
				ScheduledFuture<?> sche = (ScheduledFuture<?>) taskValue.get("sche");
				description = (String) taskValue.get("description");
				strFixedTime = (String) taskValue.get("strFixedTime");
				period = (Long) taskValue.get("period");
				initDelay = (Long) taskValue.get("initDelay");
				type = (Integer) taskValue.get("type");
				if (performType == 0) {
					//任务执行完 毕，将返回true
					if (sche.isDone()) {
						note = "任务执行完 毕.已停止.";
						status = true;
					} else {
						note = "任务执行中...";
					}
				} else if (performType == 1) {
					//true表示如果定时任务在执行，立即中止，false则等待任务结束后再停止
					sche.cancel(false);
					note = "等待任务结束后再停止.";
				}
			}
			//获得Runnable对象的执行状态
			taskInfo.setStatus(status);
			taskInfo.setDescription(description);
			taskInfo.setTaskName(key);
			taskInfo.setNote(note);
			taskInfo.setInitDelay(initDelay);
			taskInfo.setPeriod(period);
			taskInfo.setType(type);
			taskInfo.setStrFixedTime(strFixedTime);
			listTaskInfos.add(taskInfo);
		}
		return listTaskInfos;
	}

}
